Skip to content

Get the highest available version from the picker #6614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions kitsune/sumo/static/sumo/js/showfor.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,37 +106,43 @@ ShowFor.prototype.ensureSelect = function ($select, type, product, val) {
}

if (type === 'version') {
target = select(this.data.versions[product], val);
const exactVersionVal = 'version:' + val;
const exactMatch = $select.find('option[value="' + exactVersionVal + '"]');

// Handle case where version isn't found but might be newer than known versions
if (target === null && this.data.versions[product]?.length > 0) {
const versionMatch = val.match(VERSION_PATTERN);
if (versionMatch) {
const [, productPrefix, versionStr] = versionMatch;
const requestedVersion = parseInt(versionStr, 10);

let latestKnownVersion = null;
let latestVersionNum = 0;

this.data.versions[product].forEach(function (version) {
const knownVersionMatch = version.slug.match(VERSION_PATTERN);
if (knownVersionMatch && knownVersionMatch[1] === productPrefix) {
const knownVersionNum = parseInt(knownVersionMatch[2], 10);
if (knownVersionNum > latestVersionNum) {
latestVersionNum = knownVersionNum;
latestKnownVersion = version;
}
}
});
if (exactMatch.length > 0) {
$select.val(exactVersionVal);
return;
}

const versionMatch = val.match(VERSION_PATTERN);
if (versionMatch) {
const [, productPrefix, versionStr] = versionMatch;
const browserVersion = parseInt(versionStr, 10);

let highestOption = null;
let highestVersionNum = 0;

// If requested version is newer, use latest known version as base
if (latestKnownVersion && requestedVersion > latestVersionNum) {
target = { ...latestKnownVersion };
target.max_version = requestedVersion;
$select.find('option').each(function () {
const optVal = $(this).val();
const optSlug = optVal.split(':')[1];
const optMatch = optSlug.match(VERSION_PATTERN);

if (optMatch && optMatch[1] === productPrefix) {
const optVersion = parseInt(optMatch[2], 10);
if (optVersion > highestVersionNum) {
highestVersionNum = optVersion;
highestOption = optVal;
}
}
});

if (highestOption && browserVersion > highestVersionNum) {
$select.val(highestOption);
return;
}
}

target = select(this.data.versions[product], val);
if (target !== null) {
extra['data-min'] = target.min_version;
extra['data-max'] = target.max_version;
Expand Down